@layout("/common/_container.html"){
<div class="layui-card">
  <div class="layui-tab layui-tab-brief" lay-filter="compoment-tabs-brief">
	 	<ul class="layui-tab-title">
	 		<li class="layui-this" value="0">全部消息</li>
	 		<li value="1">用户消息</li>
	 		<li value="2">服务消息</li>
	 		<li value="3">支付消息</li>
	 	</ul>
  <div class="layui-card-body">
  	<div class="layui-inline" style="padding-bottom:15px">
  		<button class="layui-btn layuiadmin-btn-useradmin layui-btn-danger" data-type="bathDel">删除</button>
  		<button class="layui-btn layuiadmin-btn-useradmin" data-type="read">标记已读</button>
  		<button class="layui-btn layuiadmin-btn-useradmin" data-type="allRead">全部已读</button>
  	</div>
  <table class="layui-hidden" id="table" lay-filter="table"></table>
	</div>
  </div>
</div>


<script>
layui.config({
	base: Feng.ctxPath + "/static/layuiadmin/" //静态资源所在路径
}).extend({
	index: 'lib/index' //主入口模块
}).use(['index', 'layer','table', 'form','element'],function(){
	
	var $ = layui.$
  	,form = layui.form
  	,table = layui.table
  	,element=layui.element;
	var loadingIndex = layer.msg('数据加载中', {icon: 16, time: 0 });
	var msgType=$(".layui-tab-title .layui-this").attr("value");
	table.render({
	    elem: '#table'
	    ,url: Feng.ctxPath + '/msgContent/list'
	    ,cellMinWidth: 100
	    ,height: 'full-168' //设置高度
	    ,cols: [[
	    	{type:'checkbox'},
	        {title: '标题内容', field: 'msgContent'},
	        {title: '创建时间', field: 'createTime',width:200},
	        {title: '类型', field: 'msgType', width:140},
	        {title:'是否已读',field:'ifRead',width:140},
	    ]]
	    ,page:true
	    ,loading:true
	    ,text: {
	        none: '暂无相关数据' 
	      },
	      where:{
	    	  msgType:msgType
	    	  
	      }
	    ,done: function(res){
	    	layer.close(loadingIndex);
        }
	});
	
	//事件
	var active = {
		//加载
	   	reload: function(){
	   		var msgType=$(".layui-tab-title .layui-this").attr("value");
	       var loadingIndex = layer.msg('数据加载中', {icon: 16, time: 0 });
	      // alert("msgType为:"+msgType);
	       //执行表格重载
	       table.reload('table', {
	    	 height: 'full-168' //设置高度
	    	  ,where: {
	    		 msgType: msgType
	         } 
	       ,page:true
	       ,done: function(res){
		    	layer.close(loadingIndex);
	        }
	      });
	    },
	    //批量删除
	    bathDel:function(){
	    	var checkStatus=table.checkStatus('table'),
	    	  checkDatas=checkStatus.data;
	    	if(checkDatas.length>0 && checkDatas!=null){
	    		var msgContentIds=new Array();
	    		$.each(checkDatas,function(i,n){
	    			msgContentIds.push(n.msgContentId);
	    		});
	    		layer.confirm("确定要删除所选中的消息?",function(index){
	    			var loadWait=layer.load(2);
	    			var ajax=new $ax(Feng.ctxPath+"/msgContent/delete",function(data){
	    				layer.close(loadWait);
	    				Feng.success("删除成功!");
	    				table.reload('table',{
	    					done:function(res){
	    						layer.close(loadingIndex);
	    					}
	    				});
	    				//关闭弹层
	    				layer.close(index);
	    			},function(data){
	    				layer.close(loadWait);
	    				Feng.error("删除失败"+data.responseJson.message+"!");
	    			},true);
	    			ajax.set("msgContentIds",msgContentIds.join());
	    			ajax.start();
	    		})
	    	}else{
	    		Feng.error("操作失败! 请至少选择一项!");
	    	 }
	    },
	    
	    read:function(){
	    	var checkStatus=table.checkStatus('table'),
	    	checkDatas=checkStatus.data;
	    	if(checkDatas.length>0&&checkDatas!=null){
	    		var msgContentIds=new Array();
	    		for(i=0;i<checkDatas.length;i++){
	    			msgContentIds[i]=checkDatas[i].msgContentId;
	    		}
	    		layer.confirm("确定要标记所选的消息为已读吗?",function(index){
	    			var loadingWait=layer.load(2);
	    			var ajax=new $ax(Feng.ctxPath+"/msgContent/read",function(data){
	    				layer.close(loadingWait);
	    				Feng.success("消息标记为已读");
	    				table.reload('table',{
	    					done:function(){
	    					}	
	    				});
	    				if(data.data>0){
	    					parent.$('#msgcount').text(data.data);
	    				}
	    				layer.close(index);
	    			},function(data){
	    				layer.close(loadingWait);
	    				Feng.error("消息标记已读失败"+data.responseJson.message+"!");
	    			},true);
	    			ajax.set("msgContentIds",msgContentIds.join(','));
	    			ajax.start();
	    		})
	    	}else{	
	    		Feng.error("操作失败!请至少选择一项!");
	    	}
	    },
	    allRead:function(){
	    	var msgType=$(".layui-tab-title .layui-this").attr("value");
	    	var msghtml=$('.layui-tab-title .layui-this').html();
	    	layer.confirm("是否标记所有"+msghtml+"为已读?",function(index){
    			var loadingWait=layer.load(2);
    			var ajax=new $ax(Feng.ctxPath+"/msgContent/allread/"+msgType,function(data){
    				layer.close(loadingWait);
    				Feng.success("所有"+msghtml+"标记为已读");
    				table.reload('table',{
    					done:function(){
    					}	
    				});
    				if(data.data>0){
    					parent.$('#msgcount').text(data.data);
    				}
    				layer.close(index);
    			},function(data){
    				layer.close(loadingWait);
    				Feng.error("消息标记已读失败"+data.responseJson.message+"!");
    			},true);
    			ajax.start();
    		})
	    }
   };
	//监听选项卡
	element.on('tab(compoment-tabs-brief)',function(){
		active['reload'].call(this);
	});
  
	 $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
	     var type = $(this).data('type');
	     active[type] ? active[type].call(this) : '';
	   });
 //监听工具条
   table.on('tool(table)', function(obj){
     var data = obj.data;
     if(obj.event === 'del'){
    	 layer.confirm('确定删除此消息内容吗？', function(index){
             var loadingIndex = layer.msg('数据加载中', {icon: 16, time: 0 ,shade: 0.01});
    		 var ajax = new $ax(Feng.ctxPath + "/msgContent/delete", function (data) {
                 Feng.success("删除成功!");
                 table.reload('table',{
               	  done: function(res){
          		    	layer.close(loadingIndex);
          	          }
                  }); //数据刷新
                 layer.close(index); //关闭弹层
             }, function (data) {
                 Feng.error("删除失败!" + data.responseJSON.message + "!");
             },true);
             ajax.set("msgContentId", data.id);
             ajax.start();
         });
     }else if(obj.event === 'edit'){
       var loadingIndex = layer.msg('数据加载中', {icon: 16, time: 0 });
       layer.open({
         type: 2
         ,title: '消息内容用户'
         ,content: Feng.ctxPath + '/msgContent/msgContent_update/'+data.id
         ,area: ['720px', '390px']
         ,btn: ['确定', '取消']
         ,yes: function(index, layero){
           var iframeWindow = window['layui-layer-iframe'+ index]
           ,submitID = 'msgContent-submit'
           ,submit = layero.find('iframe').contents().find('#'+ submitID);

           //监听提交
           iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
               var loading2 = layer.load(2);
             var field = data.field; //获取提交的字段
             var ajax = new $ax(Feng.ctxPath + "/msgContent/update", function (data) {
                 Feng.success("修改成功!");
                 table.reload('table',{
               	  done: function(res){
          		    	layer.close(loadingIndex);
          	          }
                  }); //数据刷新
                 layer.close(loading2); //关闭弹层
                 layer.close(index); //关闭弹层
             }, function (data) {
                 Feng.error("修改失败!" + data.responseJSON.message + "!");
             },true);
             ajax.setData(field);
             ajax.start();
             
           });  
           
           submit.trigger('click');
         }
         ,btn2: function(index, layero){
    	   layer.close(loadingIndex);
         }
         ,cancel:function(index, layero){
    	   layer.close(loadingIndex);
         }
       });
     }
   });
 
});

   
</script>
@}
